//
//  UserModel.swift
//  swfitTest
//
//  Created by hncy on 2022/7/5.
//

//struct GroceryProduct: Codable {
//    var name: String
//    var points: Int
//    var description: String?
//}
//
//let json = """
//{
//    "name": "Durian",
//    "points": 600,
//    "description": "A fruit with a distinctive scent."
//}
//""".data(using: .utf8)!
//
//let decoder = JSONDecoder()
//let product = try decoder.decode(GroceryProduct.self, from: json)
//
//print(product.name) // Prints "Durian"


import Foundation

let localUserSaveKey = "localUserSaveKey"

struct UserModel: Codable {
    var name: String?
    var pwd: String?
    
    
    static func isLogin() -> Bool {
        let shared = UserDefaults.standard
        if let _ = shared.value(forKey: localUserSaveKey) {
            return true
        }else {
            return false
        }
    }
    
    // 保存用户信息
    static func saveUser(_ user: UserModel) {
        let shared = UserDefaults.standard
        
        // 将数据转位json存储
        
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        let jsonData = try! encoder.encode(user)
        
        let jsonString = String(bytes: jsonData, encoding: String.Encoding.utf8)
        
        shared.setValue(jsonString, forKey: localUserSaveKey)
    }
    static func loginOut() {
        let shared = UserDefaults.standard
        shared.removeObject(forKey: localUserSaveKey)
    }
}

